package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.activity.domain.entity.RdFeedBack;
import com.dsj.gxrd.modules.activity.mapper.RdFeedbackMapper;
import com.dsj.gxrd.modules.activity.service.RdFeedbackService;
import com.dsj.gxrd.utils.BaseEntityHandlerUtils;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RoleConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.Assert;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author songpin566
 * @date 2023/10/17
 **/
@Service
public class RdFeedbackServiceImpl extends ServiceImpl<RdFeedbackMapper, RdFeedBack> implements RdFeedbackService {

    @Autowired
    private RdFeedbackMapper feedbackMapper;

    @Override
    public List<RdFeedBack> selectFeedbackList(RdFeedBack feedBack) {
        return feedbackMapper.selectFeedBackList(feedBack, SecurityUtils.getLoginUser().getRoles(), SecurityUtils.getLoginUser().getSysUser().getDeptId());
    }

    @Override
    public boolean addFeedBack(RdFeedBack feedBack) {
        BaseEntityHandlerUtils.fastSetBaseEntity(feedBack, SecurityUtils.getLoginUser(), false);
        feedBack.setDelFlag(0);
        feedBack.setCurrentRoleKey(RoleConstants.VILLAGES_REP_KEY);
        feedBack.setUserId(SecurityUtils.getLoginUser().getUserid());
        feedBack.setStatus(1);
        feedBack.setIsPublic(Constants.N);
        feedBack.setProposeTime(LocalDate.now());
        return save(feedBack);
    }

    @Override
    public boolean transfer(RdFeedBack feedBack) {
        RdFeedBack dataSource = feedbackMapper.selectById(feedBack.getId());
        checkPermission(dataSource);
        dataSource.setCurrentRoleKey(feedBack.getCurrentRoleKey());
        return updateById(dataSource);
    }


    @Override
    public boolean isPublic(Long id) {
        RdFeedBack feedBack = feedbackMapper.selectById(id);
        checkPermission(feedBack);
        //发布
        feedBack.setIsPublic(Constants.Y);
        return updateById(feedBack);
    }

    private void checkPermission(RdFeedBack feedBack) {
        String isPublic = feedBack.getIsPublic();
        String currentRoleKey = feedBack.getCurrentRoleKey();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if (CollectionUtils.isEmpty(roles) || !roles.contains(currentRoleKey)) {
            throw new ServiceException("用户角色无操作权限");
        }
  /*      SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //获取当前部门所有子部门
        List<SysDept> children = deptService.selectChildrenDeptById(sysUser.getDeptId());
        Set<Long> permission = children.stream().map(SysDept::getDeptId).collect(Collectors.toSet());
        permission.add(sysUser.getDeptId());
        AjaxResult info = userService.getInfo(feedBack.getUserId());
        SysUser data = (SysUser) info.get("data");
        Long deptId = data.getDeptId();
        if(!permission.contains(deptId)){
            //不是其子部门则不允许修改
            throw new ServiceException("用户无操作权限");
        }  */
        //如果已经处理并且已经发布不允许再次操作此数据
        if (Constants.Y.equalsIgnoreCase(isPublic) && feedBack.getStatus() == 3) {
            throw new ServiceException("已经处理并且已经发布不允许再次操作此数据");
        }
    }

    @Override
    public boolean handle(RdFeedBack feedBack) {
        RdFeedBack dataSource = feedbackMapper.selectById(feedBack.getId());
        if(dataSource.getStatus() == 3){
            throw new ServiceException("此消息已经处理完毕");
        }
        checkPermission(dataSource);
        dataSource.setStatus(feedBack.getStatus());
        return updateById(dataSource);
    }


}
